$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'member/configmemberlevel/list',
        datatype: "json",
        colModel: [
            { label: '会员ID', name: 'id', index: 'id', width: 10 ,align: 'center', hidden:true},
			{ label: '等级名称', name: 'name', index: 'name', width: 50 ,align: 'center'},
            { label: '所需积分', name: 'point', index: 'point', width: 50 ,align: 'center'},
            { label: '会员折扣率', name: 'disCount', index: 'dis_count', width: 50 ,align: 'center'},
            { label: '是否默认', name: 'defaultLv', index: 'default_lv', width: 50 ,align: 'center',
                formatter:function(value, options, row){
                    // 1 男  0 女
                    if (value == 1){
                        return '是';
                    }else if (value == 0){
                        return '否';
                    }
                }},
            { label: '经验值', name: 'experience', index: 'experience', width: 50 ,align: 'center'},
            { label: '设置', name: 'edit', index: 'edit', width: 50 ,align: 'center',
                formatter:function(value, options, row){
                    var actions = [];
                    actions.push('<a href="javascript:void(0);" onclick="return edit('+ options.rowId +');">编辑</a>') ;
                    return actions.join(' ');
                }
            },
            { label: '设为默认', name: 'defaultLv', index: 'defaultLv', width: 50 ,align: 'center',
                formatter:function(value, options, row){

                    if (value == 1){
                        return '默认';
                    }else if (value == 0){
                        var actions = [];
                        actions.push('<a href="javascript:void(0);" onclick="return setDefault('+ options.rowId +');">设为默认</a>') ;
                        return actions.join(' ');
                    }
                }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", //pageNo
            rows:"limit", //pageSize
            order: "order" //asc
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

/**
 * 等级编辑
 */
function edit(rowId) {
    var rowData = $("#jqGrid").jqGrid('getRowData', rowId);
    vm.edit(rowData.id);
}

/**
 * 设为默认
 */
function setDefault(rowId) {
    var rowData = $("#jqGrid").jqGrid('getRowData', rowId);
    vm.setDefault(rowData.id);
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showListUpdate: false,
		title: null,
		configMemberLevel: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.showList = false;
            vm.showListUpdate = true,
            vm.title = "添加等级";
			vm.configMemberLevel = {};
		},
        edit: function(id){
            vm.showList = false;
            vm.showListUpdate = true;
            vm.title = "会员等级编辑";
            vm.getInfo(id)
        },
        setDefault: function(id){
            var url = "member/configmemberlevel/setDefault/"+id;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: {"id":id},
                success: function(r){
                    if(r.code > 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },
		saveOrUpdate: function (event) {
			var url = vm.configMemberLevel.id == null ? "member/configmemberlevel/save" : "member/configmemberlevel/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.configMemberLevel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "member/configmemberlevel/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "member/configmemberlevel/info/"+id, function(r){
                vm.configMemberLevel = r.configMemberLevel;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showListUpdate = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');//getGridParam获取jsonReader的值
            $("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");//trigger(“reloadGrid”)重新加载当前表格，也会向服务器发起新的请求
		}
	}
});